<template>
  <el-dialog
    :title="title"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form ref="formRef" :model="form" label-width="200px" v-if="open_type != 'psw'">
      <el-form-item label="站点名称" required prop="title">
        <el-input class="max-w460" v-model="form.title" placeholder="请输入站点名称"></el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="description">
        <el-input
          type="textarea"
          class="max-w460"
          v-model="form.address"
          placeholder="请输入站点描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="坐标" prop="" required>
        <div class="map-box">
          <el-input
            class="max-w460"
            v-model="form.longitude"
            placeholder="请输入经度"
            style="margin-right: 10px"
          ></el-input>
          <el-input class="max-w460" v-model="form.latitude" placeholder="请输入纬度"></el-input>
          <el-button @click="openMap" style="margin-left: 10px" type="primary"
            >从地图上选择</el-button
          >
        </div>
      </el-form-item>
      <el-form-item label="负责人" required>
        <el-select class="max-w460" v-model="form.manager_id" placeholder="请选择负责人">
          <el-option
            v-for="item in managers"
            :key="item.manager_id"
            :label="item.name"
            :value="item.manager_id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="站点状态" required>
        <el-select class="max-w460" v-model="form.state" placeholder="请选择站点状态">
          <el-option label="正常运行" value="active"></el-option>
          <el-option label="已停用" value="inactive"></el-option>
          <el-option label="维护中" value="under_maintenance"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="cancelFunc">取消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="loading">确定</el-button>
    </template>
  </el-dialog>
  <Map ref="mapRef" @pick="pick"></Map>
</template>

<script lang="ts" setup>
import { addProblemApi, addSiteApi, updateProblemApi, updateSiteApi } from "@/api/babycar";
import { ElMessage } from "element-plus";
import Map from "./map.vue";
import { inject, ref, watch } from "vue";
const title = ref("编辑站点");
const dialogVisible = ref(false);
const mapRef = ref<any>(null);
const managers = inject("managers") as any[];
const form = ref({
  station_id: 0,
  title: "",
  address: "",
  latitude: "",
  longitude: "",
  manager_id: 0,
  state: "active",
});
const formRef = ref<any>(null);
const loading = ref(false);
const dialogFormVisible = () => {
  dialogVisible.value = false;
};
const open_type = ref("");
const open = (type: string, row?: any) => {
  open_type.value = type;
  console.log(row);
  if (type == "edit") {
    title.value = "编辑站点";
    form.value = {
      station_id: row.station_id,
      title: row.title,
      address: row.address,
      latitude: row.latitude,
      longitude: row.longitude,
      manager_id: row.manager_id,
      state: row.state,
    };
  } else if (type == "add") {
    title.value = "添加站点";
    form.value = {
      station_id: 0,
      title: "",
      address: "",
      latitude: "",
      longitude: "",
      manager_id: 0,
      state: "active",
    };
  }
  dialogVisible.value = true;
};
const emits = defineEmits(["doneSave"]);
const onSubmit = () => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      if (open_type.value == "add") {
        addSiteApi(form.value)
          .then((data: any) => {
            ElMessage({
              message: "添加成功",
              type: "success",
            });
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      } else if (open_type.value == "edit") {
        updateSiteApi(form.value)
          .then((data: any) => {
            ElMessage({
              message: "修改成功",
              type: "success",
            });
            loading.value = false;
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      }
    }
  });
};
/*取消*/
const cancelFunc = () => {
  loading.value = false;
  dialogFormVisible();
};
const openMap = () => {
  mapRef.value.open(form.value.longitude, form.value.latitude);
};
const pick = (data: any) => {
  console.log(data);
  form.value.longitude = data.lng;
  form.value.latitude = data.lat;
  form.value.address = data.address;
};
defineExpose({ open });
</script>

<style lang="scss" scoped>
.map-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
